'''

@author: noako
'''

from GeometryHelper import Point
from FuzzyFloat import FuzzyFloat
from tkinter.constants import BOTH
try:
    # for Python2
    from Tkinter import Tk, Canvas
    from tkFileDialog import askopenfilename
except ImportError:
    # for Python3
    from tkinter import Tk, Canvas
    from tkinter.filedialog import askopenfilename

def ParseInput():
    #Tk().withdraw()
    filename = askopenfilename()
    return_value = []
    try:
        input_file = open(filename)
    except IOError:
        return None,None
    n = int(input_file.readline())
    for i in range(n):
        input_line = input_file.readline().split()
        input_line_numbers = [float(i) for i in input_line]
        point = Point(x = FuzzyFloat(input_line_numbers[0]), y = FuzzyFloat(input_line_numbers[1]))
        speed = FuzzyFloat(input_line_numbers[2])
        return_value.append((point, speed))
    return return_value,filename
    
def ParseOutput(filename, dcel):
    #TODO: Ask Maor if x and y have the same range for scaling.
    #TODO: use max and min function to make this look nicer 
    output_file = open(filename,'w+')
    output_line = str(len(dcel.vertices))+' '+str(len(dcel.faces))+'\n'
    output_file.write(output_line)
    index=0
    max_height=0;
    max_width=0;
    min_height=dcel.vertices[0].point.y;
    min_width=dcel.vertices[0].point.x;
    for vertex in dcel.vertices:
        output_line = str(vertex.point.x)+' '+str(vertex.point.y)+'\n'
        output_file.write(output_line)
        vertex.index = index
        if vertex.point.x > max_width:
            max_width = vertex.point.x
        if vertex.point.y > max_height:
            max_height = vertex.point.y
        if vertex.point.x < min_width:
            min_width = vertex.point.x
        if vertex.point.y < min_height:
            min_height = vertex.point.y
        index+=1  
        
    for face in dcel.faces:
        outer = face.outer_component
        if (outer!=None):
            output_line = str(outer.origin.index)+ ' '
            curr = outer.next
            while (outer!=curr):
                output_line = output_line + str(curr.origin.index) +' '
                curr = curr.next
        for edge in face.inner_components:
            output_line = output_line+ str(edge.origin.index)+ ' '
            curr = edge.next
            while (edge!=curr):
                output_line = output_line + str(curr.origin.index) +' '
                curr = curr.next
        output_line = output_line + '\n'
        output_file.write(output_line)
        output_line = ""
    output_file.close()
    
    dcel.center(min_width, min_height)
    factor = dcel.scale(min_width, max_width)
    dcel.moveBy(25)
    master = Tk()
    canvas = Canvas(master, width=(max_width-min_width)*factor+50, height=(max_height-min_height)*factor+50)
    for edge in dcel.edges:
        canvas.create_line(edge.origin.point.x, edge.origin.point.y,
                           edge.twin.origin.point.x, edge.twin.origin.point.y)
    canvas.pack(fill=BOTH, expand=1)
    master.mainloop()
    